package com.example.lala.mvp.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lala.R;
import com.example.mylibrary.mvp.view.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

public class LoginActivity extends BaseActivity {
    private android.widget.EditText et1;
    private android.widget.EditText et2;
    private android.widget.Button bt1;
    private android.widget.Button bt2;
    String name;
    String pwd;

    @Override
    public int bondLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        pwd = intent.getStringExtra("pwd");
        et1.setText(name);
        et2.setText(pwd);

        //登陆功能
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String ,String> map = new HashMap<>();
                if(et1!=null){
                    map.put("username",name);
                    map.put("password",pwd);
                }
                String trim = et1.getText().toString().trim();
                String trim1 = et2.getText().toString().trim();
                map.put("username",trim);
                map.put("password",trim1);
                OkGo.<String>post("https://www.wanandroid.com/user/login ")
                        .params(map)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                String body = response.body();
                                Toast.makeText(LoginActivity.this, ""+body, Toast.LENGTH_SHORT).show();;
                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                        });

            }
        });
        //跳到注册
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity
                .class));
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void Toast(String s) {

    }
}
